home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / mlregres / mlrdemo1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-09-21  |  11.4 KB  |  386 lines

  1. unit Mlrdemo1;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, DBTables, Mlrquery, DB, Mlrtable, StdCtrls, Buttons,
  8.   Grids, DBGrids, ExtCtrls, DBCtrls, OgTable, OgQuery, BkMain, OgTools,
  9.   Menus, OgMain, FwdMain, StepMain, Optigrss;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     bbtnClose: TBitBtn;
  14.     bbtnTable: TBitBtn;
  15.     Label1: TLabel;
  16.     TableSource: TDataSource;
  17.     QuerySource: TDataSource;
  18.     DBGrid1: TDBGrid;
  19.     bbtnQuery: TBitBtn;
  20.     StringGrid1: TStringGrid;
  21.     Label4: TLabel;
  22.     Panel1: TPanel;
  23.     Label2: TLabel;
  24.     Label3: TLabel;
  25.     Label11: TLabel;
  26.     Panel4: TPanel;
  27.     Label5: TLabel;
  28.     Label6: TLabel;
  29.     Label7: TLabel;
  30.     Label8: TLabel;
  31.     Label9: TLabel;
  32.     Label10: TLabel;
  33.     Panel2: TPanel;
  34.     MLRTable1: TMLRTable;
  35.     MLRQuery1: TMLRQuery;
  36.     bbtnOutput: TBitBtn;
  37.     bbtnPlots: TBitBtn;
  38.     ComboBox1: TComboBox;
  39.     MLRTable2: TMLRTable;
  40.     MLRTable3: TMLRTable;
  41.     bbtnOptimize: TBitBtn;
  42.     MLRTable4: TMLRTable;
  43.     Memo1: TMemo;
  44.     DBMemo1: TDBMemo;
  45.     SampleDataSource: TDataSource;
  46.     SampleDataTable: TTable;
  47.     MLRQuery2: TMLRQuery;
  48.     MLRQuery3: TMLRQuery;
  49.     MLRTable5: TMLRTable;
  50.     ComboBox2: TComboBox;
  51.     OptimizeMLR1: TOptimizeMLR;
  52.     BackElimMLR1: TBackElimMLR;
  53.     ComboBox3: TComboBox;
  54.     FwdSelectMLR1: TFwdSelectMLR;
  55.     Table1: TTable;
  56.     MLRQuery4: TMLRQuery;
  57.     StepWiseMLR1: TStepWiseMLR;
  58.     MLRQuery5: TMLRQuery;
  59.     procedure bbtnTableClick(Sender: TObject);
  60.     procedure bbtnQueryClick(Sender: TObject);
  61.     procedure bbtnOutputClick(Sender: TObject);
  62.     procedure bbtnPlotsClick(Sender: TObject);
  63.     procedure ComboBox1Change(Sender: TObject);
  64.     procedure bbtnCloseClick(Sender: TObject);
  65.     procedure Enablebbtn(var bbtn1,bbtn2: TBitBtn);
  66.     procedure bbtnOptimizeClick(Sender: TObject);
  67.     procedure FormActivate(Sender: TObject);
  68.     procedure SelectModelNumber(Sender: TObject);
  69.     procedure ComboBox2DropDown(Sender: TObject);
  70.     procedure FormCreate(Sender: TObject);
  71.     procedure ComboBox3Change(Sender: TObject);
  72.  
  73.   private
  74.     { Private declarations }
  75.   public
  76.     { Public declarations }
  77.   end;
  78.  
  79. var
  80.   Form1: TForm1;
  81.   Table: TMLRTable;
  82.   Query: TMLRQuery;
  83.  
  84.   QueryModels,TableModels: TMLRModels;
  85.  
  86.   count: integer;
  87.  
  88. implementation
  89.  
  90. {$R *.DFM}
  91.  
  92. procedure TForm1.bbtnTableClick(Sender: TObject);
  93. var i,j: integer;
  94. begin
  95.  DBMemo1.Visible := False;
  96.  StringGrid1.Enabled := Table.Active;
  97.  Panel2.Caption := Table.TableName;
  98.  with Table do
  99.   begin
  100.    if Reload then
  101.     begin
  102.      Execute;   { perform regression and allow access to results}
  103.      bbtnPlots.Enabled := True;
  104.      bbtnOutput.Enabled := True;
  105.      ComboBox3.Enabled := True;
  106.      bbtnOptimize.Enabled := (ComboBox3.ItemIndex >= 0)
  107.     end;  //if Reload
  108.     Label2.Caption := intToStr(n)+' datapoints.';
  109.     Label3.Caption := intToStr(DegreesOfFreedom)+' degrees of freedom.';
  110.     Label11.Caption := intToStr(nControls)+' control variables.';
  111.     StringGrid1.ColCount := nControls + 3;
  112.     StringGrid1.RowCount := n + 7;
  113.     StringGrid1.Cells[1,0] := ResponseVarY.OutputTerm;
  114.     StringGrid1.Cells[0,n+1] := 'Mean';
  115.     StringGrid1.Cells[0,n+2] := 'Std Dev';
  116.     StringGrid1.Cells[0,n+3] := 'Max';
  117.     StringGrid1.Cells[0,n+4] := 'Min';
  118.     StringGrid1.Cells[0,n+5] := 'Parameters';
  119.     StringGrid1.Cells[0,n+6] := 'Constant';
  120.     StringGrid1.Cells[1,n+1] := floatToStrF(yBar,ffFixed,7,3);
  121.     StringGrid1.Cells[1,n+2] := floatToStrF(yStdErr,ffFixed,7,3);
  122.     StringGrid1.Cells[1,n+3] := floatToStrF(yMax,ffFixed,7,3);
  123.     StringGrid1.Cells[1,n+4] := floatToStrF(yMin,ffFixed,7,3);
  124.     StringGrid1.Cells[1,n+5] := '1.00';
  125.     StringGrid1.Cells[2,n+6] := floatToStrF(b[0],ffFixed,7,3);
  126.     for j:= 1 to nControls do
  127.      begin
  128.       StringGrid1.Cells[j+2,0] := cVariable[j].OutputTerm;
  129.       StringGrid1.Cells[j+2,n+1] := floatToStrF(xBar[j],ffFixed,7,3);
  130.       StringGrid1.Cells[j+2,n+2] := floatToStrF(xStdErr[j],ffFixed,7,3);
  131.       StringGrid1.Cells[j+2,n+3] := floatToStrF(xMax[j],ffFixed,7,3);
  132.       StringGrid1.Cells[j+2,n+4] := floatToStrF(xMin[j],ffFixed,7,3);
  133.       StringGrid1.Cells[j+2,n+5] := floatToStrF(b[j],ffFixed,7,3);
  134.      end;      { for j := 1}
  135.     for i:= 1 to n do
  136.      begin
  137.       StringGrid1.Cells[0,i] := intToStr(i);
  138.       StringGrid1.Cells[1,i] := floatToStrF(y[i],ffFixed,7,3);
  139.       for j:= 1 to nControls do
  140.        StringGrid1.Cells[j+2,i] := floatToStrF(x[i,j],ffFixed,7,3);
  141.      end    { for i := 1}
  142.   end  // with Table do
  143. end;
  144.  
  145. procedure TForm1.bbtnQueryClick(Sender: TObject);
  146. var i,j: integer;
  147. begin
  148.  DBMemo1.Visible := False;
  149.  StringGrid1.Enabled := Query.Active;
  150.  Panel2.Caption := Query.SQL.Strings[0];
  151.  with Query do
  152.   begin
  153.    if ReLoad then
  154.     begin
  155.      Execute;
  156.      bbtnPlots.Enabled := True;
  157.      bbtnOutput.Enabled := True;
  158.      ComboBox3.Enabled := True;
  159.      bbtnOptimize.Enabled := (ComboBox3.ItemIndex >= 0)
  160.     end;
  161.     Label2.Caption := intToStr(n)+' datapoints.';
  162.     Label3.Caption := intToStr(DegreesOfFreedom)+' degrees of freedom.';
  163.     Label11.Caption := intToStr(nControls)+' control variables.';
  164.     StringGrid1.ColCount := nControls + 3;
  165.     StringGrid1.RowCount := n + 6;
  166.     StringGrid1.Cells[1,0] := ResponseVarY.OutputTerm;
  167.     StringGrid1.Cells[0,n+1] := 'Mean';
  168.     StringGrid1.Cells[0,n+2] := 'Std Dev';
  169.     StringGrid1.Cells[0,n+3] := 'Max';
  170.     StringGrid1.Cells[0,n+4] := 'Min';
  171.     StringGrid1.Cells[1,n+1] := floatToStrF(yBar,ffFixed,7,3);
  172.     StringGrid1.Cells[1,n+2] := floatToStrF(yStdErr,ffFixed,7,3);
  173.     StringGrid1.Cells[1,n+3] := floatToStrF(yMax,ffFixed,7,3);
  174.     StringGrid1.Cells[1,n+4] := floatToStrF(yMin,ffFixed,7,3);
  175.     for j:= 1 to nControls do
  176.      begin
  177.       StringGrid1.Cells[j+2,0] := cVariable[j].OutputTerm;
  178.       StringGrid1.Cells[j+2,n+1] := floatToStrF(xBar[j],ffFixed,7,3);
  179.       StringGrid1.Cells[j+2,n+2] := floatToStrF(xStdErr[j],ffFixed,7,3);
  180.       StringGrid1.Cells[j+2,n+3] := floatToStrF(xMax[j],ffFixed,7,3);
  181.       StringGrid1.Cells[j+2,n+4] := floatToStrF(xMin[j],ffFixed,7,3);
  182.      end;
  183.     for i:= 1 to n do
  184.      begin
  185.       StringGrid1.Cells[0,i] := intToStr(i);
  186.       StringGrid1.Cells[1,i] := floatToStrF(y[i],ffFixed,7,3);
  187.       for j:= 1 to nControls do
  188.        StringGrid1.Cells[j+2,i] := floatToStrF(x[i,j],ffFixed,7,3);
  189.      end
  190.    end
  191. end;
  192.  
  193. procedure TForm1.bbtnOutputClick(Sender: TObject);
  194. begin
  195.  if bbtnTable.Enabled then
  196.   Table.Output
  197.  else
  198.   Query.Output
  199. end;
  200.  
  201. procedure TForm1.bbtnPlotsClick(Sender: TObject);
  202. begin
  203.  if bbtnTable.Enabled then
  204.   Table.ShowPlots
  205.  else
  206.   Query.ShowPlots
  207. end;
  208.  
  209. procedure TForm1.Enablebbtn(var bbtn1,bbtn2: TBitBtn);
  210. begin
  211.  bbtn1.Enabled := True;
  212.  bbtn2.Enabled := False
  213. end;
  214.  
  215. procedure TForm1.ComboBox1Change(Sender: TObject);
  216. begin
  217.  bbtnOptimize.Enabled := False;
  218.  bbtnOutput.Enabled := False;
  219.  bbtnPlots.Enabled := False;
  220.  StringGrid1.Enabled := False;
  221.  ComboBox2.Enabled := False;
  222.  with ComboBox1 do
  223.   case ItemIndex of
  224.    0: begin
  225.        Enablebbtn(bbtnTable,bbtnQuery);
  226.        Table := MLRTable1;
  227.        SetTableModel(TableModels[1],Table);
  228.       end;
  229.    1: begin
  230.        Enablebbtn(bbtnQuery,bbtnTable);
  231.        Query := MLRQuery1;
  232.        SetQueryModel(QueryModels[1],Query);
  233.       end;
  234.    2: begin
  235.        Enablebbtn(bbtnTable,bbtnQuery);
  236.        Table := MLRTable2;
  237.        SetTableModel(TableModels[2],Table);
  238.       end;
  239.    3: begin
  240.        Enablebbtn(bbtnTable,bbtnQuery);
  241.        Table := MLRTable3;
  242.        SetTableModel(TableModels[3],Table);
  243.       end;
  244.    4: begin
  245.        Enablebbtn(bbtnTable,bbtnQuery);
  246.        Table := MLRTable4;
  247.        SetTableModel(TableModels[4],Table);
  248.       end;
  249.    5: begin
  250.        Enablebbtn(bbtnQuery,bbtnTable);
  251.        Query := MLRQuery2;
  252.        SetQueryModel(QueryModels[2],Query);
  253.       end;
  254.    6: begin
  255.        Enablebbtn(bbtnQuery,bbtnTable);
  256.        Query := MLRQuery3;
  257.        SetQueryModel(QueryModels[3],Query);
  258.       end;
  259.    7: begin
  260.        Enablebbtn(bbtnTable,bbtnQuery);
  261.        Table := MLRTable5;
  262.        SetTableModel(TableModels[5],Table);
  263.       end;
  264.    8: begin
  265.        Enablebbtn(bbtnQuery,bbtnTable);
  266.        Query := MLRQuery4;
  267.        SetQueryModel(QueryModels[4],Query);
  268.       end;
  269.    9: begin
  270.        Enablebbtn(bbtnQuery,bbtnTable);
  271.        Query := MLRQuery5;
  272.        SetQueryModel(QueryModels[5],Query);
  273.       end;
  274.   end;  { case }
  275.  Memo1.Visible := False;
  276.  DBMemo1.Visible := True;
  277.  SampleDataTable.FindKey([ComboBox1.ItemIndex]);
  278.  Screen.Cursor := crHourGlass;
  279.  if bbtnTable.Enabled then
  280.   begin
  281.    Table.Close;
  282.    Table.DatabaseName := ExtractFilePath(Application.ExeName);
  283.    OptimizeMLR1.Table := Table;
  284.    BackElimMLR1.Table := Table;
  285.    FwdSelectMLR1.Table := Table;
  286.    StepWiseMLR1.Table := Table;
  287.    TableSource.Dataset := Table;
  288.    Table.Open;
  289.    DBGrid1.DataSource := TableSource
  290.   end
  291.  else
  292.   begin
  293.    Query.Close;
  294.    Query.DatabaseName := ExtractFilePath(Application.ExeName);
  295.    OptimizeMLR1.Query := Query;
  296.    BackElimMLR1.Query := Query;
  297.    FwdSelectMLR1.Query := Query;
  298.    StepWiseMLR1.Query := Query;
  299.    QuerySource.Dataset := Query;
  300.    Query.Open;
  301.    DBGrid1.DataSource := QuerySource
  302.   end;
  303.  Screen.Cursor := crDefault;
  304. end;
  305.  
  306. procedure TForm1.bbtnCloseClick(Sender: TObject);
  307. begin
  308.  Close
  309. end;
  310.  
  311. procedure TForm1.bbtnOptimizeClick(Sender: TObject);
  312. begin
  313.  if (pos('Optimal',ComboBox3.Text) > 0) then
  314.   OptimizeMLR1.Run
  315.  else if (pos('Backward',ComboBox3.Text) > 0) then
  316.   BackElimMLR1.Run
  317.  else if (pos('Forward',ComboBox3.Text) > 0) then
  318.   FwdSelectMLR1.Run
  319.  else if (pos('Stepwise',ComboBox3.Text) > 0) then
  320.   StepWiseMLR1.Run;
  321.  if bbtnTable.Enabled then
  322.   bbtnTableClick(Sender);
  323.  if bbtnQuery.Enabled then   
  324.   bbtnQueryClick(Sender);  {
  325.  msg := 'This project uses a demo version of'+#10#13+
  326.         'Optigress.'+#10#13+#10#13+
  327.         'Optigress is a separate product, and'+#10#13+
  328.         'is NOT included with MLRegress.'+#10#13+#10#13+
  329.         'Contact Applied Analytic Systems'+#10#13+
  330.         'for ordering instructions.';
  331.  MessageDlg(msg,mtInformation,[mbOK],0); }
  332.  bbtnOptimize.Enabled := False;
  333.  ComboBox2.Enabled := True;
  334.  DBMemo1.Update
  335. end;
  336.  
  337. procedure TForm1.FormActivate(Sender: TObject);
  338. begin
  339.  SampleDataTable.Close;
  340.  SampleDataTable.DatabaseName := ExtractFilePath(Application.ExeName);
  341.  SampleDataTable.Open;
  342. end;
  343.  
  344. procedure TForm1.SelectModelNumber(Sender: TObject);
  345. var modelno: integer;
  346. begin
  347.  modelno := 1;
  348.  if (ComboBox2.ItemIndex >= 0) then
  349.   modelno := 1 + ComboBox2.ItemIndex;
  350.  OptimizeMLR1.ModelNumber := modelno;
  351.  if bbtnTable.Enabled then
  352.   bbtnTableClick(Sender);
  353.  if bbtnQuery.Enabled then
  354.   bbtnQueryClick(Sender)
  355. end;
  356.  
  357. procedure TForm1.ComboBox2DropDown(Sender: TObject);
  358. var i: integer;
  359. begin
  360.  ComboBox2.Items.Clear;
  361.  for i := 1 to OptimizeMLR1.ModelsSaved do
  362.   ComboBox2.Items.Add(intToStr(i))
  363. end;
  364.  
  365. procedure TForm1.FormCreate(Sender: TObject);
  366. begin
  367.  SaveTableModel(TableModels[1],MLRTable1);
  368.  SaveTableModel(TableModels[2],MLRTable2);
  369.  SaveTableModel(TableModels[3],MLRTable3);
  370.  SaveTableModel(TableModels[4],MLRTable4);
  371.  SaveTableModel(TableModels[5],MLRTable5);
  372.  SaveQueryModel(QueryModels[1],MLRQuery1);
  373.  SaveQueryModel(QueryModels[2],MLRQuery2);
  374.  SaveQueryModel(QueryModels[3],MLRQuery3);
  375.  SaveQueryModel(QueryModels[4],MLRQuery4);
  376.  SaveQueryModel(QueryModels[5],MLRQuery5);
  377. end;
  378.  
  379. procedure TForm1.ComboBox3Change(Sender: TObject);
  380. begin
  381.  bbtnOptimize.Enabled := True
  382. end;
  383.  
  384.  
  385. end.
  386.